
import * as assetTypeService from '../../services/assetType';

import * as assetGroupService from '../../services/assetGroup';
import * as assetAttrService from '../../services/assetAttr';

export default {
  namespace: 'assetAttr',
  state: {
    assetTypeTree: [],
    current: 1,
    currentSize: 10,
    total: 0,
    assetTypeId:'',
    content:[],
    activityKey:'',
    assetGroupList: [],
    attrList: [],
    currentItem: {},
    currentAttrItem:{},
    removeAttr:"assetAttr/removeAttr",
    qryAttrById:"assetAttr/qryAttrById",
    modalVisible : false,
    modalType : 'create',
    attrModalType:'createAttr',
    newKey : '',
    attrModalVisible:false,
    panes: [],
    isShowOptions:undefined,
  },
  reducers: {
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
        return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },
    addPaneSuc(state, { payload }) {
        state.panes.push(payload);
        return { ...state };
    },
    removePaneSuc(state, { payload }) {
        const panes = state.panes.filter(pane => pane.key !== payload);
        return { ...state, panes };
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
        return { ...state, assetTypeTree };
    },
    queryAssetAttrByTypeIdSuc(state, { payload }) {
        var assetTypeId=payload.assetTypeId;
        var panes = (payload.panes && payload.panes.length)?payload.panes:"";
        var activityKey=panes[0].id_;
        if(panes){
          var index=0
          panes = panes.map(d=>{
            index++;
            return {title: d.name, key:d.id_,closable:true,}
          })
        }
      return { ...state, panes ,assetTypeId,activityKey};
    },
    NoPanesSuc(state, { payload }) {
        var assetTypeId=payload.assetTypeId;
        var panes = [];
        var content =[];       
         return { ...state, panes ,content,assetTypeId};
    },
   changeTab(state,{payload}){
        return { ...state, ...payload};
   },
   showAttrModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , attrModalVisible: true }
   },
   hideAttrModal (state) {
        return { ...state, attrModalVisible: false, isShowOptions:undefined,newKey : new Date().getTime() + '' }
   },
   removeAttrSuc (state,{payload}){
        return { ...state, ...payload}
   },
   reloadAttrSuc (state,{payload}){
        return { ...state, ...payload}
   },
   onOptionChange (state, { payload }) {
     return { ...state, isShowOptions:payload}
   },


  },
  effects: {
    *create({ payload },{ call, put }) {
      const { body }  = yield call(assetGroupService.create,payload);
      yield put({ type: 'reload',payload });
      yield put({ type: 'hideModal' });
    },
    *removePane({ payload },{ call, put }) {
      const { body }  = yield call(assetGroupService.remove,payload.groupId);
      yield put({ type: 'queryAssetAttrByTypeId',  payload:{assetTypeId:payload.assetTypeId} });
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(assetTypeService.queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(assetGroupService.qryById,payload);
      yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
            modalType: 'edit',
            title: '编辑属性组',
          },
      });
    },
    *edit({ payload },{ call, put }) {
      const { body }  = yield call(assetGroupService.edit,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload',payload });

    },
    *queryAssetAttrByTypeId({ payload },{ call, put }) {
      const { body } = yield call(assetGroupService.queryAssetAttrByTypeId,payload);
      if(body.data){
        if(body.data.length>0){
           yield put({ type: 'queryAssetAttrByTypeIdAndGroup', payload:{assetTypeId:payload.assetTypeId,groupId:body.data[0].id_}});
           yield put({ type: 'queryAssetAttrByTypeIdSuc', payload: { panes:body.data, assetTypeId:payload.assetTypeId }});
        }else{
           yield put({ type: 'NoPanesSuc', payload: { assetTypeId:payload.assetTypeId }});
        }
       
      }     
    },
    *reload({ payload },{ call, put }) {
      yield put({ type: 'queryAssetAttrByTypeId', payload:{assetTypeId:payload.assetTypeId}});
    },

    *changeTabEvent({ payload },{ call, put }) {
      const { body } = yield call(assetGroupService.queryAssetAttrByTypeIdAndGroup,payload);
      yield put({ type: 'reloadAttrSuc', payload:{content:body.data}});
      yield put({ type: 'changeTab', payload:{activityKey:payload.groupId}});
    },

    *queryAssetAttrByTypeIdAndGroup({ payload },{ call, put }) {
      const { body } = yield call(assetGroupService.queryAssetAttrByTypeIdAndGroup,payload);
      yield put({ type: 'reloadAttrSuc', payload:{content:body.data}});
    },

     //添加属性
    *createAttr({ payload },{ call, put }) {
      const { body }  = yield call(assetAttrService.create,payload);
      yield put({ type: 'queryAssetAttrByTypeIdAndGroup',payload });
      yield put({ type: 'hideAttrModal' });
    },

    //删除属性
   *removeAttr({ payload },{ call, put }) {
      const { body }  = yield call(assetAttrService.remove,payload.id);
      yield put({ type: 'queryAssetAttrByTypeIdAndGroup',payload});
      yield put({ type: 'removeAttrSuc', payload });
    },

   *qryAttrById({ payload },{ call, put }){
      const { body } = yield call(assetAttrService.qryById,payload);
      yield put({
          type: 'showAttrModal',
          payload: {
            currentAttrItem: body.data,
            isShowOptions: body.data.type,
            attrModalType: 'editAttr',
            title: '编辑属性',
          },
      });
    },
    *editAttr({ payload },{ call, put }) {
      const { body }  = yield call(assetAttrService.edit,payload);
      yield put({ type: 'hideAttrModal' });
      yield put({ type: 'queryAssetAttrByTypeIdAndGroup',payload });

    },


  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/basic/assetAttr') {
          dispatch({ type: 'queryAssetType' });
        }
      });
    },
  },
};
